<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class C_Main extends MY_Controller {

	function __construct()
	{
		parent::__construct();
                $this->load->helper('html');
                $this->load->helper('url');
	}

	function index(){
            $this->load->view('v_login');
//            $this->load->view('v_inicio');
	            
	}
        
        function validaEntrada(){
            
            $user = strtolower(stripslashes($_POST['usuario']));
            $passwd = $_POST['senha'];
            
            $usuario = $this->em->getRepository('models\Usuario')
                         ->findOneBy(array('usuarioNome' => $user));
            
            if($usuario){
                
                if(crypt($passwd,$usuario->getSenhaUser()) == $usuario->getSenhaUser()){
                    
                    $separador = '';
                    $strAcesso = '';
                    $acessoList = $this->em->createQuery('SELECT r FROM models\Regra_Acesso r WHERE r.perfilCod = ?1');
                    $acessoList->setParameter(1, $usuario->getPerfilUser());

                    foreach ( $acessoList->getResult() as $regra ) {	

                            $strAcesso .= $separador . $regra->getRegraId();
                            $separador = ',';
                    }
                    
                    $this->session->set_userdata ('usuario', $usuario->getNomeUser());
                    $this->session->set_userdata ('perfil', $usuario->getPerfilUser());
                    $this->session->set_userdata ('usuarioId', $usuario->getIdUser());
                    $this->session->set_userdata ('acessoList', $strAcesso);
                    
                    $this->m_log->buildLog('O usuario '.$this->session->userdata['usuario'].' acessou o sistema.',  Acao_Const::ACESSO_SISTEMA);
                         
                    
                    $this->load->view('menu/i_menu');
                }
                
                
            } else {
                
                echo FALSE;
                
            }

            
        }

        
	
}